﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Control_Searcher;
using Telerik.WinControls.UI;
using System.IO;
using System.Configuration;

namespace Text_Finder
{
    public partial class FilVælger : Telerik.WinControls.UI.RadForm
    {
        public bool nextPressed { get; private set; }
        public FilVælger()
        {
            InitializeComponent();
            
        }

        

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            fillTree();
        }
        string slnPath;
        private void fillTree()
        {
            //finder solution filen og derefter alle projekter og de klasser der findes i projektet
            Configuration config = ConfigurationManager.OpenExeConfiguration(@"LanguageApp.exe");
            slnPath = config.AppSettings.Settings["SlnFile"].Value; //use constants
            Solution solu= new Solution(slnPath);
            string sol = new FileInfo(slnPath).DirectoryName;
            foreach (SolutionProject p in solu.Projects)
            {
                RadTreeNode parent = new RadTreeNode();
                parent.ShowCheckBox = false;
                parent.Text = p.ProjectName;
                parent.Value = p;
                DirectoryInfo ProjectDir = new FileInfo(sol + "\\" + p.RelativePath).Directory;
                FileInfo[] fi = ProjectDir.GetFiles();
                foreach (FileInfo file in fi)
                {
                    if (file.Name.IndexOf(".cs", StringComparison.OrdinalIgnoreCase) > 0 && file.Name.IndexOf(".designer.cs", StringComparison.OrdinalIgnoreCase) < 0 && file.Name.IndexOf(".csproj", StringComparison.OrdinalIgnoreCase) < 0)
                    {
                        RadTreeNode child = new RadTreeNode();
                        child.Text=file.Name;
                        child.Value=file;
                        parent.Nodes.Add(child);
                    }
                }
                radTreeView1.Nodes.Add(parent);
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {//make it go .. instead
            string sol =  @"..\..\..\Control Searcher\SubjectsTemplate.cs";//new FileInfo(slnPath).DirectoryName +
            string savingPath =  @"..\..\..\Control Searcher\Subjects.cs";
            List<FileInfo> files = new List<FileInfo>();
            Dictionary<string, string> namespaces = new Dictionary<string, string>();
            List<string> forms = new List<string>();
            List<SolutionProject> includeProjects = new List<SolutionProject>();
           
            
            foreach(RadTreeNode p in radTreeView1.Nodes) //finder former der skal behandles samt husker det projekt de er i så referencer kan blive givet
            {
                bool addedyet = false;
                foreach (RadTreeNode c in p.Nodes)
                {
                    if (c.Checked)
                    {
                        files.Add((FileInfo)c.Value);
                        if (!addedyet)
                        {
                            addedyet = true;
                            includeProjects.Add((SolutionProject)p.Value);
                        }
                    }
                    
                }
            }
            if (files.Count > 0) { 
            foreach (FileInfo addThis in files)
            {
                List<string> n = new List<string>();
                List<string> f = ClassParser.findFormNamesAndNameSpaces(addThis.FullName,out n); //finder navnet på former og deres namespaces
                //navnet på formerne bruges til at genererer instansen mens namespaces bliver brugt til at indsætte using namespace.
                foreach (string ns in n)
                {
                    if (!namespaces.ContainsKey(ns))
                        namespaces.Add(ns, ns);
                }
                forms.AddRange(f.ToArray());
                
            }
            System.IO.File.Copy(@"..\..\..\Control Searcher\Control Searcher.csproj", @"..\..\..\Control Searcher\Control Searcher.csproj.Original", true);
            StringBuilder ProjectLines = new StringBuilder();
            string pline = string.Empty;
            System.IO.StreamReader pfile = new System.IO.StreamReader(@"..\..\..\Control Searcher\Control Searcher.csproj");
            bool done = false;
            while((pline = pfile.ReadLine())!=null) //giver controlsearcher referencer til de forms/vinduer den skal behandle
            {
                if (pline.Contains("<ProjectReference Include=") && !done)
                {
                    foreach (SolutionProject p in includeProjects)
                    {
                        ProjectLines.AppendLine("    <ProjectReference Include=\"..\\"+p.RelativePath+"\">");
                        ProjectLines.AppendLine("      <Project>"+p.ProjectGuid+"</Project>");
                        ProjectLines.AppendLine("      <Name>"+p.ProjectName+"</Name>");
                        ProjectLines.AppendLine("    </ProjectReference>");
                    }
                    done = true;
                }
                    ProjectLines.AppendLine(pline);
            }
            pfile.Close();
            System.IO.File.WriteAllText(@"..\..\..\Control Searcher\Control Searcher.csproj", ProjectLines.ToString());
            
            StringBuilder FileLines = new StringBuilder();
            foreach (string ns in namespaces.Values)
            {
                FileLines.AppendLine("using "+ns+";");
            }
            string line = string.Empty;
            System.IO.StreamReader file = new System.IO.StreamReader(sol);
            while ((line = file.ReadLine()) != null)
            {//to insert usings
                if (line.Contains("    public class SubjectsTemplate"))
                   line= line.Replace("    public class SubjectsTemplate", "    public class Subjects");
                FileLines.AppendLine(line);
                if (line.Contains("//--**linesaddstart"))
                   foreach (string addThis in forms)//to insert all formnames + get namespaces for usings
                    {
                        
                        FileLines.AppendLine("            forms.Add(new "+addThis+"());");
                    }
               
                else if(line.Contains("//--**stringsaddstart"))
                   
                    foreach (FileInfo addThis in files)
                    {
                        FileLines.AppendLine("            forms.Add(@\""+addThis.FullName+"\");");
                    }
                
            }
            file.Close();
            System.IO.File.WriteAllText(savingPath, FileLines.ToString());
            nextPressed = true;
            this.Hide();
            }
        }


        
        
    }
}
